package com.bawei.module_home.discovery

import android.view.LayoutInflater
import androidx.recyclerview.widget.LinearLayoutManager
import com.bawei.library_common.mvvm.BaseMvvmFragment
import com.bawei.library_network.newFactory.ApiEmptyResponse
import com.bawei.library_network.newFactory.ApiErrorResponse
import com.bawei.library_network.newFactory.ApiSuccessResponse
import com.bawei.module_home.R
import com.bawei.module_home.databinding.HomeFragmentDiscoveryBinding
import com.bawei.module_home.discovery.adapter.DiscoveryAdapter
import com.bawei.module_home.discovery.entity.DiscoveryEntity
import com.bawei.module_home.discovery.viewmodel.DiscoveryViewModel
import com.blankj.utilcode.util.ToastUtils
import com.gyf.immersionbar.ImmersionBar

/**
 * 发现页面
 */

class DiscoveryFragment : BaseMvvmFragment<DiscoveryViewModel,HomeFragmentDiscoveryBinding>() {
    private lateinit var discoveryAdapter: DiscoveryAdapter
    override fun getLayout(): Int {
        return R.layout.home_fragment_discovery
    }

    override fun onResume() {
        super.onResume()
        ImmersionBar
            .with(this)
            .statusBarColor(R.color.purple_200)
            .init()
    }

    override fun initData() {

        //初始化列表信息
        discoveryAdapter = DiscoveryAdapter()
        viewDataBinding.rvDiscovery.apply {
            adapter = discoveryAdapter
            layoutManager = LinearLayoutManager(context)
            setHasFixedSize(true)  // 确定Item的改变不会影响RecyclerView的宽高
        }
        discoveryAdapter.addFooterView(LayoutInflater.from(context).inflate(R.layout.home_item_foote_view,viewDataBinding.rvDiscovery,false))
        //设置loadSir
        setLoadSir(viewDataBinding.rvDiscovery)
        network()

    }

    private fun network() {
        showLoading()
        viewModel.discovery().observe(viewLifecycleOwner){
            when(it){
                is ApiSuccessResponse ->{
                    ToastUtils.showShort("数据请求成功")
                    discoveryAdapter.addData(it.body.itemList as MutableList<DiscoveryEntity.Item>)//添加数据源刷新适配器
                    showContent()
                }
                is ApiErrorResponse -> showFailure(it.errorMessage)
                is ApiEmptyResponse -> showEmpty()
            }
        }
    }


    override fun initListener() {

    }

    override fun getJVMClass(): Class<DiscoveryViewModel> {
        return DiscoveryViewModel::class.java
    }

    override fun onRetryBtnClick() {
        network()
    }


}